/**
 * 
 */
package com.vodden.money.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.vodden.money.client.LedgerService;
import com.vodden.money.shared.Account;
import com.vodden.money.shared.Entry;

/**
 * @author VoddeRM1
 * 
 */

public class LedgerServiceImplementation extends RemoteServiceServlet implements
		LedgerService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.vodden.money.client.LedgerService#getLedgerEntries()
	 */
	@Override
	public ArrayList<Entry> getLedgerEntries() {
		@SuppressWarnings("unused")
		Account account;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Entry.class);
		query.setOrdering("date asc");
		@SuppressWarnings("unchecked")
		ArrayList<Entry> results = new ArrayList<Entry>((List<Entry>) query.execute());
		// touch each account object to ensure retrieval before the PM is closed.
		// FIX: there must be a better way of doing it
		for ( Entry entry : results ) {
			account = entry.getAccount();
		}
		pm.close();
		return results;
	}

}
